#ifndef Example_H
#define Example_H

#include "FrameListener.h"
#include "KeyListener.h"
#include "MouseListener.h"
#include "Image.h"
#include "Core.h"
#include "Bullets.h"
#include "PhysicObject.h"
#include <vector>

class PhysicBox
{
	public:
		PhysicBox(int x, int y, SDL_Surface* img, b2World& world, float friction = 1.0)
		:img(img)
		{

            bodyDef.position.Set(x, y);
            body = world.CreateBody(&bodyDef);
            // Define another box shape for our dynamic body.
            shapeDef.SetAsBox(img->w/2, img->h/2);
            // Set the box density to be non-zero, so it will be dynamic.
            shapeDef.density = 200.0f;
            // Override the default friction.
            shapeDef.friction = 0.5f;
            //shapeDef.restitution = 0.05f;
            // Add the shape to the body.
            body->CreateShape(&shapeDef);
            // Now tell the dynamic body to compute it's mass properties base
            // on its shape.
            body->SetMassFromShapes();
            img = SDL_DisplayFormat(img);

		};
		void Draw(SDL_Surface* screen)
		{
		    b2Vec2 position = body->GetPosition();
            //float32 angle = body->GetAngle();

            //img = img2;


            //boxRGBA(screen, position.x, position.y, position.x+img->w, position.y+img->h, 255, 255, 255, 128);
            const b2XForm& xf = body->GetXForm();


            b2PolygonShape* poly = (b2PolygonShape*)body->GetShapeList();
            int32 vertexCount = poly->GetVertexCount();
            const b2Vec2* localVertices = poly->GetVertices();

            b2Assert(vertexCount <= b2_maxPolygonVertices);
            b2Vec2 vertices[b2_maxPolygonVertices];

            for (int32 i = 0; i < vertexCount; ++i)
            {
                vertices[i] = b2Mul(xf, localVertices[i]);
            }
            SDL_Rect dstrect;
            dstrect.x = vertices[0].x;
            dstrect.y = vertices[0].y;
            for(int x = 0; x < 4; ++x)
            {
                if(vertices[x].x<dstrect.x)
                    dstrect.x = vertices[x].x;
                if(vertices[x].y<dstrect.y)
                    dstrect.y = vertices[x].y;
            }


            //dstrect.x = position.x-img->h/2;
            //dstrect.y = position.y-img->w/2;
            SDL_Surface * img2 = rotozoomSurface (img, body->GetAngle()*-57.2957796, 1, 0);
            SDL_BlitSurface(img2, 0, screen, &dstrect);
/*
            //m_debugDraw->DrawSolidPolygon(vertices, vertexCount, color);
            Sint16 verticesx[vertexCount];
            Sint16 verticesy[vertexCount];
            for(int x = 0; x< vertexCount;++x)
            {
                verticesx[x] = vertices[x].x;
                verticesy[x] = vertices[x].y;
            }
            for(int x = 0; x< vertexCount;++x)
            {
                if(x<vertexCount-1)
                {


                    aalineRGBA(screen, verticesx[x], verticesy[x],
                         verticesx[x+1], verticesy[x+1], 0, 0, 255, 255);
                }
                else
                {
                    aalineRGBA(screen, verticesx[x], verticesy[x],
                         verticesx[0], verticesy[0], 0, 0, 255, 255);
                }
            }
            filledPolygonRGBA(  screen, verticesx, verticesy, vertexCount, 0, 0, 255, 128 );*/
            //texturedPolygon(  screen, verticesx, verticesy, vertexCount, img, 0, 0 );


		};
		~PhysicBox()
		{
            SDL_FreeSurface(img);
        };
	protected:

	private:
	SDL_Surface* img;
	b2BodyDef bodyDef;
	b2Body* body;
	b2PolygonDef shapeDef;

};
class Example : public FrameListener, public KeyListener, public MouseListener
{
    public:
        Example();
        ~Example();
    protected:
    private:
        void LoadImages();

        bool FrameStarted(float timeSinceLastFrame);
        bool FrameEnded();

        bool OnKeyDown(const SDLKey& key);
        bool OnKeyUp(const SDLKey& key);

        bool OnMouseMove(const SDL_MouseMotionEvent& motion);
        bool OnMouseButtonDown(const SDL_MouseButtonEvent& button);
        bool OnMouseButtonUp(const SDL_MouseButtonEvent& button);
        Image* img;
        Image* bulletImage;
        Image* ship;
        PhysicObject* mObject;

	// Define the size of the world. Simulation will still work
	// if bodies reach the end of the world, but it will be slower.
	b2AABB worldAABB;


	// Define the gravity vector.
	b2Vec2 *gravity;


	// Construct a world object, which will hold and simulate the rigid bodies.
	b2World *world;
	std::vector<PhysicBox*> boxlist;


};

#endif // Example_H
